/*
 * Created on 22.07.2008
 *
 */
package com.appspot.yttv.client.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import com.google.gwt.user.client.Random;

/**
 * Simple class to keep a list of videos
 * @author Uwe Maurer
 *
 */
public class Playlist {
	private List<Video> playlist = new ArrayList<Video>();
	
	public boolean add(Video o) {
		List<Video> oneVideoList = new ArrayList<Video>();
		oneVideoList.add(o);
		return addAll(oneVideoList);
	}

	public boolean addAll(Collection<? extends Video> c) {
		Set<Video> mergeResult = new LinkedHashSet<Video>();
		mergeResult.addAll(playlist);
		boolean moddified = mergeResult.addAll(c);
		if (moddified) {
			playlist = new ArrayList<Video>(mergeResult);
		}
		return moddified;
	}

	public boolean equals(Object o) {
		return playlist.equals(o);
	}

	public Video get(int index) {
		return playlist.get(index);
	}

	public int hashCode() {
		return playlist.hashCode();
	}

	public boolean isEmpty() {
		return playlist.isEmpty();
	}

	public int size() {
		return playlist.size();
	}
	
	public void shuffle() {
		// Knuth shuffle.
		for (int i = playlist.size(); i > 0; --i) {
			swapEntries(i - 1, Random.nextInt(i));
		}
	}
	
	private void swapEntries(int i1, int i2) {
		Video temp = playlist.get(i1);
		playlist.set(i1, playlist.get(i2));
		playlist.set(i2, temp);
	}
}
